﻿using KinonekoSoftware.Foundation.UI.Commands;
using KinonekoSoftware.Foundation;

namespace KinonekoSoftware.Foundation.UI.ViewModels
{
    public abstract class TabViewModel : ViewModelBase, IPageRoot
    {
        private bool _removable;

        protected TabViewModel()
        {
            CloseTabCommand = CreateCommand(CloseTabCommandHandler);
            OpenTabCommand  = CreateCommand(OpenTabCommandHandler);
        }
        
        #region Command Handlers

        
        private void OpenTabCommandHandler()
        {
            (Ioc.Get<ShellCore>() as TabbedShell)?.OpenOutboardTab(this);
        }
        
        private async void CloseTabCommandHandler()
        {
            //
            // 避免在Xaml中忘记设置Visibility而意外关闭
            if (!Removable)
            {
                return;
            }
            
            await this.Close(this);
        }

        #endregion

        #region OnReceive

        
        protected sealed override void OnReceive(object parameter)
        {
            var dc = (PageContext)parameter;
            OnReceive(dc);
        }

        protected virtual void OnReceive(PageContext context)
        {
            
        }

        #endregion
        
        public bool Removable
        {
            get => _removable;
            set => SetValue(ref _removable, value);
        }
        
        public ICommandEX CloseTabCommand { get; }
        public ICommandEX OpenTabCommand  { get; }
    }
}